package org.languageguide.readings;

import java.util.ArrayList;
import java.util.List;

/* Wraps sentences in spans with (class="sentence")
 *  
 *  Expects Latin character style punctuation found in English or other European language 
 *  
 */

public class StandardSentenceWrapper implements SentenceWrapper {

	private int location = 0;
	private List<List<String>> paragraphs;

	public String wrap(String toPrepare){
		loadDocumentIntoParagraphsAndSentences(toPrepare);
		return generateText();
	}
	
	private String generateText(){
		int sentenceCounter = 0;
		StringBuilder text = new StringBuilder();
		for (List<String> paragraph: paragraphs){
			text.append("<p>");
			for (String sentence: paragraph){
				text.append("<span id=\"s" + sentenceCounter  + "\">");
				text.append(sentence);
				text.append("</span> ");
				sentenceCounter++;
			}
			text.append("</p>");
		}
		return text.toString();
	}
	
	
	private void loadDocumentIntoParagraphsAndSentences(String toPrepare){
		StringBuilder builder = new StringBuilder(toPrepare);
		paragraphs = new ArrayList<List<String>>();
		while (hasMore(location, builder)){
			char i = builder.charAt(location);
			if (i == '<'){
				String tagName = retrieveTagNameAndAdvanceLocation(builder);
				if (tagName.equals("p")){
					List<String> sentences;
					try {
						sentences = processParagraphAndAdvanceLocation(builder);
						paragraphs.add(sentences);
					} catch (Exception e) {
						System.out.println("exception" + e);
						e.printStackTrace();
						return;
					}
				}
				else{
					location++;
				}
			}
			else{
				location++;
			}
		}
	}
	
	private boolean hasMore(int location, StringBuilder builder){
		if (location < builder.length())
			return true;
		else return false;
	}
	
	/**
	 * Throws an exception if paragraph is not closed with a </p>
	 * 
	 * @param builder
	 * @return
	 * @throws Exception
	 */
	private List<String> processParagraphAndAdvanceLocation(StringBuilder builder) throws Exception{
		List<String> sentences = new ArrayList<String>();
		StringBuilder sentence = new StringBuilder();
		while (hasMore(location, builder)){
			char i = builder.charAt(location);
			if (i == '<'){ // check to see if we've encounted a paragraph tag.
				String tagName = retrieveTagNameAndAdvanceLocation(builder);
				if (tagName.equals("/p")){
					return sentences;
				}
				if (tagName.equals("p")){
					throw new Exception("Paragraphh doesn't have closing </p>");
				}
			}
			else if (isPunctuation(i) && notPartOfAbreviationOrNumber(builder)){
				sentence.append(i);
				sentences.add(sentence.toString().trim());
				sentence = new StringBuilder();
				//location++; // skip over space that comes after punctuation
			}
			else if (i == ' '){
				if (abreviationEncountered)
					abreviationEncountered = false;
				sentence.append(i);
			}
			else sentence.append(i);
			location++;
		}
		
		// if you get here it means that the paragraph isn't closed with a </p>
		// let's return the sentences for now - it might be better to throw an exception
		throw new Exception("Document doesn't have closing </p>");
		//return sentences;
	}
	
	private boolean isPunctuation(int i){
		if (i == '.' || i == '?' || i == ';' || i == '!'){
			return true;
		}
		else return false;
	}
	
	boolean abreviationEncountered = false;;
	public boolean notPartOfAbreviationOrNumber(StringBuilder builder){
		if (!abreviationEncountered){
			char characterAfterPunctuation = builder.charAt(location + 1);
			if (characterAfterPunctuation == ' ' || characterAfterPunctuation == '<'){
				return true;
			}
			abreviationEncountered = true;
		}
		return false;
	}
	
	
	private String retrieveTagNameAndAdvanceLocation(StringBuilder builder){
		location = location + 1;
		char cha = builder.charAt(location);
		StringBuilder tag = new StringBuilder(""); 
		while (cha == ' '){
			location++;	
			cha = builder.charAt(location);
		}
		while (cha != ' ' && cha != '>'){
			tag.append(cha);
			//System.out.println(tag);
			location++;	
			cha = builder.charAt(location);
		}
		location++;
		return tag.toString();
	}

	
	
	
	public static void main(String[] args){
		String paragraph = "<p>Although the Yapese people usually spend U.S. dollars, their traditional money, the Rai, is actually made of stone.</p> ";
		
		SentenceWrapper wrapper = new StandardSentenceWrapper();
		String processed = wrapper.wrap(paragraph);
		System.out.println(processed);
	}
	
}
